package com.xiaolin.dao;

import com.xiaolin.pojo.User;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @author XiaoLin
 * @date 2019/2/22 20:59
 */
@org.apache.ibatis.annotations.Mapper
public interface UserMapper extends Mapper<User> {

    @Select("SELECT * FROM user WHERE username = #{username} AND `password` = #{password}")
    User loginName(User user);

    @Select("SELECT * FROM user WHERE phone = #{username} AND `password` = #{password}")
    User loginPhone(User user);

    @Select("SELECT * FROM user WHERE uid = #{userId}")
    @Results(id = "user", value = {
            @Result(property = "uid", column = "uid"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "email", column = "email"),
            @Result(property = "myinfo", column = "myinfo"),
            @Result(property = "uimage", column = "uimage"),
            @Result(property = "username", column = "username"),
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "address", column = "address"),
    })
    User selectFriendById(String userId);

    @Select("<script> SELECT * FROM user WHERE " +
            "uid != #{myid} AND uid NOT IN (" +
            "SELECT my_friend_uid FROM friends WHERE my_uid = #{myid})" +
            "<if test='uid!=null'> AND ( uid = #{uid} or nick_name like #{username} ) </if></script>"
    )
    @ResultMap("user")
    List<User> findRecoUser(@Param("uid") String uid, @Param("username") String username, @Param("myid") String myid);
}
